 //
//  MPTLiveLoginVC.m
//  MPTLive
//
//  Created by HouGeng on 16/1/27.
//  Copyright © 2016年 Jeakin. All rights reserved.
//

#import "MPTLiveLoginVC.h"
#import <JLRoutes.h>
#import "MPTTips.h"
#import "MPTLoginUser.h"
#import "MPTHttpClient.h"
#import "MPTLiveLoginProtocolVC.h"
#import "NSString+MPTString.h"
#import "WXApi.h"


#define TP_4S_H 480
#define TP_5_H 568
#define TP_6_H 667
#define TP_6P_H 736


@interface MPTLiveLoginVC ()

@property (nonatomic, assign) LoginType loginType;

@end


@implementation MPTLiveLoginVC


#pragma mark - ********************** View Lifecycle **********************

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initVariable]; /// 初始化变量
    [self initViews]; /// 初始化界面
    
    [self regNotification]; /// 注册消息
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    // 一定要关注这个函数是否被执行了！！！
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


#pragma mark - ********************** Init All Views **********************

- (void)initVariable
{
    // 登录成功消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:@"kLoginSuccess" object:nil];
    // 登录失败消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginFail:) name:@"kLoginFail" object:nil];
}

- (void)initViews
{
    /// 根据界面情况，决定是单独写函数，或直接写Views
    [self initMainViews];
}

- (void)initMainViews
{
    /// 背景
    /*
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    view.backgroundColor = RGBAllColor(0xffe200);
    [self.view addSubview:view];
    
    /// icon
    UIImageView *imageIC = [[UIImageView alloc] initWithFrame:CGRectMake((screenW - 70) / 2, 161, 70, 70)];
    imageIC.image = [UIImage imageNamed:@"MPTLive_LoginIcon"];
    [self.view addSubview:imageIC];
    
    /// 直播文字
    UIImageView *imageLive = [[UIImageView alloc] initWithFrame:CGRectMake((screenW - 107) / 2, CGRectGetMaxY(imageIC.frame) + 30, 107, 24)];
    imageLive.image = [UIImage imageNamed:@"MiaoPaiLive"];
    [self.view addSubview:imageLive];
     */
    
    UIImageView *imgv = [[UIImageView alloc] initWithFrame:self.view.bounds];
    if (screenH == TP_4S_H)
    {
        imgv.image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    }
    else if (screenH == TP_5_H)
    {
        imgv.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    }
    else if (screenH == TP_6_H)
    {
        imgv.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h"];
    }
    else if (screenH == TP_6P_H)
    {
        imgv.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h"];
    }
    else
    {
        imgv.image = [UIImage imageNamed:@"LaunchImage-700"];
    }
    
    [self.view addSubview:imgv];
    
    /// 盖住点东西用的
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, screenH - 80, screenW, 80)];
    view.backgroundColor = RGBAllColor(0xffe200);
    [self.view addSubview:view];
    
    /// 条款
    UILabel *lab =[[UILabel alloc] initWithFrame:CGRectMake((screenW - 225)/ 2, screenH - 45, 105, 15)];
    lab.text = @"登录即表示您同意";
    lab.font = [UIFont fontWithName:@"FZLTXHK--GBK1-0" size:12.0f];
    lab.textColor = RGBAllColor(0x726501);
    lab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lab];
    
    TPButton *but = [TPButton getWithFrame:CGRectMake(CGRectGetMaxX(lab.frame), screenH - 45, 120, 15) target:self action:@selector(TiaoKuanClicked)];
    [but setTitle:@"《秒拍直播用户协议》"forState:UIControlStateNormal];
    but.titleLabel.font = [UIFont fontWithName:@"FZLTXHK--GBK1-0" size:12.0f];
    [but setTitleColor:RGBAllColor(0xf97c02) forState:UIControlStateNormal];
    [self.view addSubview:but];
    
    /// 3个登录按钮
    CGFloat fltKong = (screenW - 230)/ 2;
    TPButton *butWeiBo = [TPButton getWithFrame:CGRectMake(fltKong, CGRectGetMinY(lab.frame) -80, 50, 50)
                                         target:self
                                         action:@selector(butWeiBo)];
    [butWeiBo setImage:[UIImage imageNamed:@"WeiBoN"] forState:UIControlStateNormal];
    [butWeiBo setImage:[UIImage imageNamed:@"WeiBoS"] forState:UIControlStateHighlighted];
    [self.view addSubview:butWeiBo];
    
    TPButton *butWeiXin = [TPButton getWithFrame:CGRectMake(CGRectGetMaxX(butWeiBo.frame) + 40,
                                                            CGRectGetMinY(lab.frame) -80, 50, 50)
                                          target:self
                                          action:@selector(butWeiXin)];
    [butWeiXin setImage:[UIImage imageNamed:@"WeiXinN"] forState:UIControlStateNormal];
    [butWeiXin setImage:[UIImage imageNamed:@"WeiXinS"] forState:UIControlStateHighlighted];
    if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi])
    {
        [self.view addSubview:butWeiXin];
    }
    else
    {
        
    }
    
    
    TPButton *butQQ = [TPButton getWithFrame:CGRectMake(CGRectGetMaxX(butWeiXin.frame) + 40,
                                                        CGRectGetMinY(lab.frame) -80, 50, 50)
                                      target:self
                                      action:@selector(butQQ)];
    [butQQ setImage:[UIImage imageNamed:@"QQN"] forState:UIControlStateNormal];
    [butQQ setImage:[UIImage imageNamed:@"QQS"] forState:UIControlStateHighlighted];
    [self.view addSubview:butQQ];
    
    /// 登录方式  4个字加线
    UILabel *labWenZi = [[UILabel alloc] initWithFrame:CGRectMake((screenW - 60)/ 2.0,
                                                                  CGRectGetMinY(butQQ.frame) -45,
                                                                  60, 15)];
    labWenZi.text = @"登录方式";
    labWenZi.textAlignment = NSTextAlignmentCenter;
    labWenZi.textColor = RGBAllColor(0x282828);
    labWenZi.font = [UIFont fontWithName:@"FZLTXHK--GBK1-0" size:15.0f];
    [self.view addSubview:labWenZi];
    
    UILabel *labLine1 = [[UILabel alloc] initWithFrame:CGRectMake((screenW - 180 - 60 - 20)/ 2.0,
                                                                  labWenZi.center.y,
                                                                  90, 0.5)];
    labLine1.backgroundColor = RGBAllColor(0x666666);
    [self.view addSubview:labLine1];
    
    UILabel *labLine2 = [[UILabel alloc] initWithFrame:CGRectMake(screenW - (screenW - 180 - 60 - 20)/ 2.0 - 90,
                                                                  labWenZi.center.y,
                                                                  90, 0.5)];
    labLine2.backgroundColor = RGBAllColor(0x666666);
    [self.view addSubview:labLine2];
    
}


#pragma mark - ********************** Functions **********************

#pragma mark - 注册消息

- (void)regNotification
{
    
}


#pragma mark - ********************** Button Touch Event **********************

- (void)btnClickBack:(TPButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

/// 微博登录
- (void)butWeiBo
{
    _loginType = WeBo;
    [JLRoutes routeURL:[NSURL URLWithString:@"/login/sso/sina"]];
}

/// QQ登录
- (void)butQQ
{
    _loginType = QQ;
    [JLRoutes routeURL:[NSURL URLWithString:@"/login/sso/qq"]];
}

/// 微信登录
- (void)butWeiXin
{
    _loginType = WeXin;
    [JLRoutes routeURL:[NSURL URLWithString:@"/login/sso/weixin"]];
}

/// 点击条款
- (void)TiaoKuanClicked
{
    MPTLiveLoginProtocolVC *vc = [[MPTLiveLoginProtocolVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - ********************** Net Connection Event **********************

/**
 *  调用白名单接口,验证某个用户是否有直播权限
 */
- (void)req_Authory
{
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSDictionary *paraDic = @{@"version":ZB_Str_Protect(version),
                              @"unique_id":ZB_Str_Protect([NSString MPTIdentifierString])
                              };
    
    [[MPTHttpClient sharedMPHTTPClient] getPath:@"m/checkLiveAuth.json" parameters:paraDic queuePriority:NSOperationQueuePriorityNormal cacheRequest:NO success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         if ([responseObject isKindOfClass:[NSDictionary class]])
         {
             if ([responseObject[@"status"] integerValue] == 200)
             {
                 /// 保存直播状态
                 [[NSUserDefaults standardUserDefaults] setValue:responseObject[@"code"] forKey:LiveCode];
             }
         }
     }
                                        failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         
     }
                                         cached:^(NSDictionary *cachedObject)
     {
         
     }];
}


#pragma mark - ********************** Delegate Event **********************





#pragma mark - ********************** Notification Event **********************

/// 登录成功
- (void)loginSuccess:(NSNotification *)note
{
    [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
    
    ///登录成功回调
    if (self.block)
    {
        /// 请求直播状态接口
        [self req_Authory];
        
        self.block();
    }
}

///登录失败了
- (void)loginFail:(NSNotification *)notification
{
    [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
    [MPTTips showSingleTips:@"用户登录失败，请重新登录" duration:1.5];
}



@end
